using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XNA_2DFramwork;
using FarseerGames.FarseerPhysics.Collisions;

namespace ArtificialIntelligence.Racing
{
    public class Police:ComputerAi
    {
        Texture2D VisualTexture;
        public float policeS;
        private RacePlayer _player;
        private Geom GeomCatch;
        Vector2 pos = new Vector2(10000, 0);
        public Police(Game game,Vector2 position,RacePlayer player,string filename,string GameOverName,PhysicsSimulator physicsSimulator):base(game)
        {
            PhysicsSimulator = physicsSimulator;
            this._player=player;
            Type = ObjectType.Police;
            
            this.Texture = game.Content.Load<Texture2D>(filename);
            this.VisualTexture = game.Content.Load<Texture2D>(GameOverName);
            Width = Texture.Width;
            Height = Texture.Height;
            Origin=new Vector2(Width/2f,Height/2f);
            Position = position;
            Body = BodyFactory.Instance.CreateRectangleBody(PhysicsSimulator, Width, Height, 1);
            Body.Position = position;
            Geom = GeomFactory.Instance.CreateRectangleGeom(PhysicsSimulator, Body, Width, Height);
            GeomCatch = GeomFactory.Instance.CreateRectangleGeom(Body,10000,10000);
            Body.IgnoreGravity = true;
            Geom.CollisionResponseEnabled = false;
            SpriteBatch = new SpriteBatch(Game.GraphicsDevice);
        }
        bool x = false;
        public override void Update()
        {
            if (x == false)
            {
                //this.Rectangle_AI = new Rectangle((int)this.Position.X, (int)this.Position.Y + 50, this.Texture.Width, this.Texture.Height + 100);
                ////this.CatchRec = new Rectangle(250, 800, 10000, 10000);
                if (VisualObject.IsCollide(ref _player.Geom,ref GeomCatch))
                {
                    if (VisualObject.IsCollide(ref _player.Geom,ref this.Geom))
                    {
                        pos.X = 0;
                        pos.Y = 0;
                        City.CurrentLenght = -10000;
                    }
                    else
                        Position.Y -= policeS - RacePlayer.playerS;
                    if (RacePlayer.playerS == 0)
                    {
                        if (this.Position.X > _player.Position.X)
                        {
                            this.Position.X -= 4f;
                            if (this.Position.X == _player.Position.X)
                            {

                                this.policeS = 0;
                                RacePlayer.playerS = 0;
                            }
                        }
                        else
                        {
                            if (_player.Position.X == this.Position.X)
                            {
                                this.Position.X += 4f;
                                if (this.Position.Y == _player.Position.Y - _player.Texture.Height)
                                {

                                    this.policeS = 0;
                                    RacePlayer.playerS = 0;
                                }
                            }
                        }
                    }
                    if (this.Position.X < _player.Position.X + _player.Texture.Width)
                        this.Position.X++;
                    else
                        this.Position.X -= 3f;
                }
           }
        }
        public override void Draw()
        {
            this.SpriteBatch.Begin();
            this.SpriteBatch.Draw(this.Texture, Body.Position, null, Color.White, Body.Rotation, Origin, 1f, SpriteEffects.None, 0f); 
            this.SpriteBatch.Draw(this.VisualTexture, this.pos, Color.White);
            this.SpriteBatch.End();
        }
        public void Move(float x, float y)
        {
            this.Position.X += x;
            this.Position.Y += y;
        }
    }
}